$(function () {
    initLayerDate("#page_begTime",false);
    initLayerDate("#page_endTime",false);
    initLayerDate("#user_begTime",false);
    initLayerDate("#user_endTime",false);
    //--------------时间，开始
    initIndexTime();
    //--------------时间，结束
    //基本数据统计-------开始
    /**
     * 加载基础数据
     * @param callback
     */
    function getAccessCountNum(callback) {
        var params = $('#page-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/getAccessCountNum',
            type:'post',
            data:params,
            contentType:'application/json;charset=utf-8',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result);
                }
            }
        });
    }
    //获取基础数据
    accessCountNum();

    $("body").on("click",".page",function(){
        accessCountNum();
    }).on('click','.user',function(){
        var params = $('#user-form').serializeJSON();
       if(params.begTime==""&&params.endTime==""){
           queryUserLoginDayTimeNum(queryUserLoginDayTimeNumCallback,"")
           return;
        }
        var oDate1 = new Date(params.begTime);
        var oDate2 = new Date(params.endTime);
         if(oDate1.getTime() == oDate2.getTime()){
             queryUserLoginDayTimeNum(queryUserLoginDayTimeNumCallback,params.endTime)
          } else {
             queryUserRegTimeNum(queryUserLoginDayTimeNumCallback);
            }

    });

    function accessCountNum(){
        //显示数据
        getAccessCountNum(function (result) {
            //PC平台总PV数
            $("#pvCountNum").text(result.pageUVpvSumNumMap.pvCountNum);
            //PC平台总UV数
            $("#uvCountNum").text(result.pageUVpvSumNumMap.uvCountNum);
            //注册用户总数
            $("#regSumUserNum").text(result.userNumMap.sumUserNum);
            //购课学员数
            $("#buyCourseUserNum").text(result.buyCourseUserNum);
            //活跃用户
            $("#activeUserNum").text(result.activeUserNum);
        });
    }

    //基本数据统计-------结束

    //--------------今日网站访问量曲线-------开始----------------

    queryUserLoginDayTimeNum(queryUserLoginDayTimeNumCallback,"")
    /**
     * 获取当日用户登录统计数据
     * @returns {*}
     */
    function queryUserLoginDayTimeNum(callback,date) {
        $.ajax({
            url: baseloaction +'/admin/count/queryUserLoginDayTimeNum',
            type:'post',
            data:{"date":date},
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result,1);
                }
            }
        });
    }

    /**
     * 获取某段时间的访问人数
     * @param callback
     */
    function queryUserRegTimeNum(callback) {
        var params = $('#user-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/queryUserLoginTimeNum',
            type:'post',
            data:params,
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success==true){
                    callback(result,2);
                }
            }
        });
    }



    function queryUserLoginDayTimeNumCallback(result,type){
        var max = result.max;
        var userLoginDayNumList = result.entity;
        var regUserNumArr = new Array();
        var nRegUserNumArr = new Array();
        var buyUserNumArr = new Array();
        var yArray = new Array();
        var hoursArray =['0:00','1:00','2:00','3:00','4:00','5:00','6:00','7:00','8:00','9:00','10:00','11:00'
            ,'12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'];
        if(type==1){
            yArray =hoursArray;
        }
        for(var i=0; i<userLoginDayNumList.length; i++){
            var obj = userLoginDayNumList[i];
            regUserNumArr.push(obj.regNum);
            buyUserNumArr.push(obj.buyNum);
            if(type==2){
                yArray.push(obj.dates)
                nRegUserNumArr.push(obj.nRegNum);
            }else{
                nRegUserNumArr.push(obj.nregNum);
            }

        }
        var lineChart = echarts.init(document.getElementById("user-echarts-login-chart"));
        var lineoption = {
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true,
            color: ['#62A1FE', '#4ECE39', '#675bba'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {
                data:['注册用户','非注册用户','付费用户']
            },
            xAxis: [
                {
                    type: 'category',
                    data: yArray,
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '今日访问',
                    min: 0,
                    max: max,
                    interval: 50,
                    axisLabel: {
                        formatter: '{value} 个'
                    }
                }
            ],
            series: [
                {
                    name:'注册用户',
                    type:'line',
                    data:regUserNumArr
                },
                {
                    name:'非注册用户',
                    type:'line',
                    data:nRegUserNumArr
                },
                {
                    name:'付费用户',
                    type:'line',
                    data:buyUserNumArr
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    }

    //--------------今日网站访问量曲线-------结束----------------

    //--------------注册对比柱型图-------开始--------------------
    /**
     * 注册对比柱型图
     * @param callback
     */
    function regUserContrastDataNum(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryRegFromUserNum',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }
    //显示加载的统计数据
    regUserContrastDataNum(function (data) {
        var lineChart = echarts.init(document.getElementById("user-echarts-reg-chart"));
        var lineoption = {
            color: ['#3398DB'],
            tooltip : {
                trigger: 'axis',
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis : [
                {
                    type : 'category',
                    data : ['后台开通','IOS注册','安卓注册','IOS-QQ','安卓-QQ','IOS-微信','安卓-微信','小程序','IOS内购','H5注册','H5-QQ','H5-微信','PC注册','PC-QQ','PC-微信','学员卡'],
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [
                {
                    name:'人数',
                    type:'bar',
                    barWidth: '60%',
                    data:[
                        data.adminFromNum, //后台开通
                        data.iosFromNum, //IOS注册
                        data.androidFromNum, //安卓注册
                        data.iosThirdQQNum, //IOS-QQ
                        data.androidThirdQQNum, //安卓-QQ
                        data.iosThirdWeiXinNum, //IOS-微信
                        data.androidThirdWeiXinNum, //安卓-微信
                        data.appletThirdWeiXinNum, //小程序
                        data.iosNum, //IOS内购
                        data.mobileFromNum, //H5注册
                        data.mobileThirdQQNum, //H5-QQ
                        data.mobileThirdWeiXinNum, //H5-微信
                        data.registerFromNum, //PC注册
                        data.pcThirdQQNum, //PC-QQ
                        data.pcThirdWeiXinNum, //PC-微信
                        data.userCardFromNum //学员卡
                    ]
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    });
    //--------------注册对比柱型图-------结束--------------------

    //-------------平台栏目-频道-访问占比----------开始----------
    /**
     * 获取平台栏目-频道-访问占比统计数据
     * @returns {*}
     */
    function getColumnAccessCountNumData(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryPageViewCountNum',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }
    //加载显示灵气
    getColumnAccessCountNumData(function (data) {
        var indexList = new Array();
        var courseList = new Array();
        var activityList = new Array();
        var examList = new Array();
        var qaList = new Array();
        var classList = new Array();
        var cmsList = new Array();
        var bookList = new Array();
        var openList = new Array();
        for(var i=0;i<data.length;i++){
            indexList.push(data[i].indexNum);
            courseList.push(data[i].courseNum);
            activityList.push(data[i].activityNum);
            examList.push(data[i].examNum);
            qaList.push(data[i].qaNum);
            classList.push(data[i].classNum);
            cmsList.push(data[i].cmsNum);
            bookList.push(data[i].bookNum);
            openList.push(data[i].openNum);
        }
        var lineChart = echarts.init(document.getElementById("column-echarts-chart"));
        var lineoption = {
            title: {
                text: '栏目访问统计图'
            },
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {
                data:['首页','课程详情','首页Banner专题','考试首页','问答首页','班级首页','资讯首页','图书列表','免费直播首页']
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis : [
                {
                    type : 'category',
                    boundaryGap : false,
                    data:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [
                {
                    name:'首页',
                    type:'line',
                    areaStyle: {normal: {}},
                    data:indexList
                },
                {
                    name:'课程详情',
                    type:'line',
                    areaStyle: {normal: {}},
                    data:courseList
                },
                {
                    name:'首页Banner专题',
                    type:'line',
                    areaStyle: {normal: {}},
                    data:activityList
                },
                {
                    name:'考试首页',
                    type:'line',
                    areaStyle: {normal: {}},
                    data:examList
                },
                {
                    name:'问答首页',
                    type:'line',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:qaList
                },
                {
                    name:'班级首页',
                    type:'line',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:classList
                },
                {
                    name:'资讯首页',
                    type:'line',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:cmsList
                },
                {
                    name:'图书列表',
                    type:'line',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:bookList
                },
                {
                    name:'免费直播首页',
                    type:'line',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    areaStyle: {normal: {}},
                    data:openList
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    });
    //-------------平台栏目-频道-访问占比----------结束----------
});